package com.zhss.springbootdemo01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName GlobalErrorHandler
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2021/2/6 22:56
 * @Version 1.0
 */
@ControllerAdvice
public class GlobalErrorHandler {
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public ErrorInfo<String> defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
		ErrorInfo<String> r = new ErrorInfo<String>();
		r.setMessage(e.getMessage());r.setCode(ErrorInfo.ERROR);
		r.setData("自定义的全局异常处理");
		r.setUrl(req.getRequestURL().toString());
		return r;
   }

}
